import Vue from 'vue'
import {getAction } from '@/api/manage'
import {getEngineerList,getDeliveryByProduceName} from '@/api/api'

export const DeliveryModalMixin = {
  data () {
    return {
      engineerList: [],
      spans: {
        labelCol1: {span: 2},
        wrapperCol1: {span: 22},
        //1_5: 分为1.5列（相当于占了2/3）
        labelCol1_5: { span: 3 },
        wrapperCol1_5: { span: 21 },
        labelCol2: {span: 4},
        wrapperCol2: {span: 20},
        labelCol3: {span: 6},
        wrapperCol3: {span: 18},
        labelCol6: {span: 12},
        wrapperCol6: {span: 12}
      },
      scanBarCode: '',
      scanStatus: true,
    }
  },
  computed: {
    readOnly: function() {
      return this.action !== "add" && this.action !== "edit";
    }
  },
 
  created() {

  },
  methods: {
    initEngineer(params) {
      let that = this;
      getEngineerList(params).then((res)=>{
        if(res) {
          that.engineerList = res;
          console.log('getengineerlist',res);
        }
      });
    },
    changeColumnShow(info) {
      if(info.barCode) {
        this.changeFormTypes(this.materialTable.columns, 'barCode', 1)
      }
      if(info.standard) {
        this.changeFormTypes(this.materialTable.columns, 'standard', 1)
      }
    
    },
     /** 查询某个tab的数据 */
     requestSubTableData(url, params, tab, success) {
      tab.loading = true
      getAction(url, params).then(res => {
        if(res && res.code === 200){

          let arr = res.data.name
          tab.dataSource = res.data.rows
          for(let item of this.materialTable.columns){
            if(item.key == 'produceId' ) {
              item.options = []
              for(let i=0; i<arr.length; i++) {
                let depotInfo = {};
                depotInfo.value = arr[i].produceId + '' //注意-此处value必须为字符串格式
                depotInfo.text = arr[i].produceName
                depotInfo.title = arr[i].produceName
                item.options.push(depotInfo)
              }
            }
          }
          typeof success === 'function' ? success(res) : ''
        }
      }).finally(() => {
        tab.loading = false
      })
    },
     
      autoChangeValue(target) {
        let width = this.form.getFieldValue('width')-0
        let height = this.form.getFieldValue('height')-0
        let length = this.form.getFieldValue('length')-0
        let unit = this.form.getFieldValue('unit')

        this.$nextTick(() => {
          this.form.setFieldsValue({'width':width,'height':height,'length':length,'unit':unit})
        });
      },
    //单元值改变一个字符就触发一次
    onValueChange(event) {
      let that = this
      const { type, row, column, value, target } = event
      let param,width,height,length,unit,price,number,total
      switch(column.key) {
        case "produceId":
          param = {
            produceId: value
          }
          console.log(event,'event');
          getDeliveryByProduceName(param).then((res) => {
            console.log(res,'getDeliveryByProduceName');
            if (res && res.code === 200) {
              if(res.data && res.data.rows) {
                let mArr = []
                let mInfo = res.data.rows[0]
                let mInfoEx = this.parseInfoToObj(mInfo)
                let mObj = {
                  rowKey: row.id,
                  values: mInfoEx
                }
                mArr.push(mObj)
                target.setValues(mArr);
                target.recalcAllStatisticsColumns()
                that.autoChangeValue(target)
              }
            }
          });
          break;
          case "price":
          case "number":
            console.log(column.key,'key');
            price = row.price-0 
            number = row.number-0 
            total = (price*number).toFixed(2)-0
            target.setValues([{rowKey: row.id, values: {total: total}}])
            target.recalcAllStatisticsColumns()
            that.autoChangeValue(target)
            break;
      }
    },
      //转为商品对象
      parseInfoToObj(mInfo) {
        return {
          width: mInfo.sizeX,
          height: mInfo.unitX,
          length: mInfo.sizeY,
          unit: mInfo.unit,
          number: 0,
          price: 0,
          total: 0
        }
      },
  }
}